package com.yun.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yun.entity.Files;
import com.yun.entity.Share;
import com.yun.entity.User;
import com.yun.service.ShareService;
import com.yun.utils.DESUtil;
import com.yun.utils.Office2PDF;




/**
 * 文件分享控制器
 * @author zhangxubin
 *
 */
@Controller
public class ShareController {

	@Resource
	private ShareService shareServiceImpl;
	@RequestMapping(value="/shareList")
	public String list(HttpSession session,Model model) {
		 User user=(User)session.getAttribute("existUser");
		List<Share> list = shareServiceImpl.findFilesShareByUserId(user.getUserId());
		model.addAttribute("shareList",list);
		return "shareList";
	}
	@RequestMapping(value="/shareFileByFileId")
	@ResponseBody
	public String shareFileByFileId(@RequestBody Files files) {
		System.out.println(files.getFileId());
		files = shareServiceImpl.select(files.getFileId());
		int insertResult= shareServiceImpl.insertFileByFileId(files);
		if(insertResult>0) {
			return "OK";
		}else
			return  "NO";
	}
	@RequestMapping(value="share")	
	public String showShare(Model model,HttpSession session) {
		List<Share> share = shareServiceImpl.findShareList();
		System.out.println(share.size());
		model.addAttribute("shareList",share);
		
		return "share";
	}
	
	//在线预览
	@RequestMapping("/onlinePreview")
	public void onlinePreview(int fileId,HttpServletResponse response){
		Files files = shareServiceImpl.select(fileId);
		System.out.println("预览的文件:"+files);
		DESUtil desUtil = new DESUtil();
		//将文件名解码
//		String filePath = desUtil.decode(shareLink);
		String filePath = files.getFileUrl()+files.getFileName();
		System.out.println("文件路径:"+filePath);
		BufferedInputStream br;
		OutputStream out;
		try {
			File file = null;
			String[] split = filePath.split("\\.");
			if (!"pdf".equals(split[split.length-1])) {//当文件格式不是.pdf
				if (!new File(filePath.substring(0,filePath.lastIndexOf("."))+".pdf").exists()) {
					file = Office2PDF.openOfficeToPDF(filePath);
				}else{
					file = new File(filePath.substring(0,filePath.lastIndexOf("."))+".pdf");
				}
			}else{
				file = new File(filePath);
			}
			System.out.println("预览文件:"+file);
			br = new BufferedInputStream(new FileInputStream(file));
			byte[] buf = new byte[1024];
			int len = 0;
			response.reset(); // 非常重要
			response.setContentType("application/pdf");
			response.setHeader("Content-Disposition",
			        "inline; filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
			out = response.getOutputStream();
			while ((len = br.read(buf)) != -1)
			    out.write(buf, 0, len);
			br.close();
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
